#!/usr/bin/env python
#    Copyright (C) 2012  Nicholas Wilde <ncwilde43@gmail.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Bring up the select file dialog once the browse button has been clicked"""

from xml.dom.minidom import parse, Document
from PyQt4.QtGui import QFileDialog, QLineEdit

def _getNodeValue(dom, nodeName):
    nodelist = dom.getElementsByTagName(nodeName)
    return str(nodelist.item(0).firstChild.data).strip()
        
def importFile(self, fileName=None):
    if not fileName:
        fileName = str(QFileDialog.getOpenFileName(self, "Select File", "data",
            "XML File (*.xml);; All Files (*.*)"))
    if fileName:
        self.clearFields()
        dom = parse(fileName)

        # Mac
        self.lineEdit_mac.setText(_getNodeValue(dom, 'mac'))
        
        # Broadcast
        self.lineEdit_broadcast.setText(_getNodeValue(dom, 'broadcast'))
        
        # Destination
        self.lineEdit_destination.setText(_getNodeValue(dom, 'destination'))
        
        # Port
        self.lineEdit_port.setText(_getNodeValue(dom, 'port'))
        
        # Destination Checkbox
        self.checkBox_destination.setCheckState(int(_getNodeValue(dom,
            'destinationCheckBox')))
        
        # Destination Checkbox
        self.checkBox_port.setCheckState(int(_getNodeValue(dom,
            'portCheckBox')))
        
# Get fileName
def exportFile(self, fileName=None):
  if not fileName:
      fileName = QFileDialog.getSaveFileName(self,
          "Save File", "untitled.xml", "XML File (*.xml)")
  if fileName:
      # Get all lineEdit values
      # From jdi http://stackoverflow.com/a/10067109/1061279
      elements = dict(
          mac = self.lineEdit_mac.text,
          broadcast = self.lineEdit_broadcast.text,
          destination = self.lineEdit_destination.text,
          port = self.lineEdit_port.text,
          destinationCheckBox = self.checkBox_destination.checkState,
          portCheckBox = self.checkBox_port.checkState
      )

      doc = Document()
      wol = doc.createElement("wol")
      doc.appendChild(wol)

      for name, fn in elements.iteritems():
          node = doc.createElement(name)
          wol.appendChild(node)

          text = str(fn())
          nodeText = doc.createTextNode(text)
          node.appendChild(nodeText)

      with open(fileName, 'w') as f:
          doc.writexml(f, indent='', addindent='  ', newl='\n')